﻿
Partial Class store_video
    Inherits CSearchPage

#Region "Data"
    Private m_products As CProductList
    Private m_products1 As CProductList
    Private m_products2 As CProductList
    Public ReadOnly Property [Products]() As CProductList
        Get
            If IsNothing(m_products) Then
                m_products = New CProductList(Products1.Count + Products2.Count)
                If String.IsNullOrEmpty(Search) Then
                    m_products.AddRange(Products2.SortByCreatedDescName)
                    m_products.AddRange(Products1.SortByCreatedDescName)
                Else
                    m_products.AddRange(Products2)
                    m_products.AddRange(Products1)
                End If
            End If
            Return m_products
        End Get
    End Property
    Public ReadOnly Property [Products1]() As CProductList
        Get
            If IsNothing(m_products1) Then
                m_products1 = CProduct.Cache.Active.Sermons.HavingVideos

                If SpeakerId > 0 Then m_products1 = m_products1.GetBySpeaker1or2Id(SpeakerId)
                If LanguageId > 0 Then m_products1 = m_products1.GetByLanguageId(LanguageId)
                m_products1 = m_products1.Search(Search)
            End If
            Return m_products1
        End Get
    End Property
    Public ReadOnly Property [Products2]() As CProductList
        Get
            If IsNothing(m_products2) Then
                m_products2 = CProduct.Cache.Active.ConferenceSets.HavingVideos

                If SpeakerId > 0 Then m_products2 = m_products2.GetBySpeaker1or2Id(SpeakerId)
                If LanguageId > 0 Then m_products2 = m_products2.GetByLanguageId(LanguageId)
                m_products2 = m_products2.Search(Search)
            End If
            Return m_products2
        End Get
    End Property
#End Region

#Region "Menu"
    Protected Overrides ReadOnly Property TopPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhTopLinks
        End Get
    End Property
    Protected Overrides ReadOnly Property SubPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhSubLinks
        End Get
    End Property
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        'Dropdowns
        For Each i As CSpeaker In CProduct.Cache.Active.Sermons.HavingVideos.Speakers
            CDropdown.Add(ddSpeaker, CUtilities.NameAndCount(CUtilities.Truncate(i.SpeakerName), i.Products.Active.HavingVideos), i.SpeakerId)
        Next
        CDropdown.BlankItem(ddSpeaker, "-- Any Speaker --", 0)
        CDropdown.SetValue(ddSpeaker, SpeakerId)

        For Each i As CLanguage In CProduct.Cache.Active.Sermons.HavingVideos.Languages
            CDropdown.Add(ddLanguage, CUtilities.NameAndCount(CUtilities.Truncate(i.NativeAndName), i.Products.Active.HavingVideos), i.LanguageId)
        Next
        CDropdown.BlankItem(ddLanguage, "-- Any Language --", 0)
        CDropdown.SetValue(ddLanguage, LanguageId)


        'Search state (from querystring)
        txtSearch.Text = Me.Search

        'Tweak MetaDesc
        If LanguageId > 0 AndAlso LanguageId <> ELanguage.English Then
            Dim lang As String = CLanguage.Cache.GetById(LanguageId).NativeAndName
            Title = String.Concat(lang, " ", Title)
            MetaDescription = String.Concat(MetaDescription, " (", lang, ")")
        End If
        If SpeakerId > 0 AndAlso SpeakerId <> ESpeaker.MikeConnell Then
            Dim speak As String = CSpeaker.Cache.GetById(SpeakerId).SpeakerName
            Title = String.Concat(Title, ": ", speak)
            MetaDescription = String.Concat(speak, ": ", MetaDescription)
        End If

        lnkDonate.NavigateUrl = CSitemap.External_Donations
        lnkVimeo.NavigateUrl = CSitemap.External_Vimeo
        lnkYouTube.NavigateUrl = CSitemap.External_YouTube
    End Sub
    Protected Overrides Sub PagePreRender()
        'Paging and display
        Dim list As CProductList = Products1
        For Each i As CProduct In ctrlPagingSermons.Display(list)
            UCVideo(plhSermons).Display(i, list)
        Next


        list = Products2
        For Each i As CProduct In ctrlPagingSets.Display(list)
            UCVideo(plhSets).Display(i, list)
        Next
        plhFirstTable.Visible = plhSets.Controls.Count > 0


        'Emphasise active filters
        If CDropdown.GetByte(ddSpeaker) > 0 Then ddSpeaker.ForeColor = Drawing.Color.DarkBlue
        If CDropdown.GetByte(ddLanguage) > 0 Then ddLanguage.ForeColor = Drawing.Color.DarkBlue
        If txtSearch.Text.Length > 0 Then txtSearch.ForeColor = Drawing.Color.DarkBlue
    End Sub
#End Region

#Region "Event Handlers - Form"
    Protected Sub btnSearch_Click(sender As Object, e As System.EventArgs) Handles btnSearch.Click, ddSpeaker.SelectedIndexChanged, ddLanguage.SelectedIndexChanged
        Response.Redirect(CSitemap.Video(txtSearch.Text, CDropdown.GetInt(ddSpeaker), CDropdown.GetInt(ddLanguage)))
    End Sub
#End Region

#Region "User Controls"
    Private Shared Function UCVideo(ByVal target As Control) As store_UCVideo
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.Store_UCVideo)
        target.Controls.Add(ctrl)
        Return CType(ctrl, store_UCVideo)
    End Function
#End Region

End Class

